package com.banyuan.servlet.author;

import com.alibaba.fastjson.JSON;
import com.banyuan.dao.AuthorDao;
import com.banyuan.domain.Author;
import com.banyuan.domain.ResData;
import com.banyuan.utils.CommonUtils;
import com.banyuan.utils.DBUtils;
import com.banyuan.utils.JDBCUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/author/updatePassword")
public class UpdatePasswordServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String password = req.getParameter("password");
        String newpassword = req.getParameter("newpassword");
        String renewpassword = req.getParameter("renewpassword");

        // 1.输入的值是不是为null或者去掉空格后为""
        if (CommonUtils.isEmpty(password) || CommonUtils.isEmpty(newpassword) ||
                CommonUtils.isEmpty(renewpassword) ) {
            ResData resData = new ResData(10001,"输入项为空",null);
            System.out.println(resData);
            resp.getWriter().write(JSON.toJSONString(resData));
            return; // 阻断下面代码的运行
        }


        // 2.两次输入的新密码是不是一样
        if (!newpassword.equals(renewpassword)) {
            ResData resData = new ResData(10002,"两次输入的新密码不一样",null);
            System.out.println(resData);
            resp.getWriter().write(JSON.toJSONString(resData));
            return; // 阻断下面代码的运行
        }



        // 3.输入的旧密码对不对,通过id去查找
        HttpSession session = req.getSession();
        Author author = (Author) session.getAttribute("author");

        Integer id = author.getId();
        ResultSet resultSet = null;
        try {
             resultSet = AuthorDao.selectAuthorById(id);
            Author author1 = DBUtils.resultToBeanOne(resultSet, Author.class);
            if (!CommonUtils.calculateMD5(password).equals(author1.getPassword())) {
                ResData resData = new ResData(10003,"旧密码不正确",null);
                System.out.println(resData);
                resp.getWriter().write(JSON.toJSONString(resData));
                return; // 阻断下面代码的运行

            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeBy(resultSet);
        }


        // 4.将密码从数据库里修改,一定要传加密后的密码

        try {
            int i = AuthorDao.updatePasswordById(author.getId(), CommonUtils.calculateMD5(newpassword));
            if (i > 0) {
                // i>0就是修改成功了，清空session，并且跳转到登陆页面

                ResData resData = new ResData(200,"密码修改成功",null);
                System.out.println(resData);
                resp.getWriter().write(JSON.toJSONString(resData));

                session.invalidate(); // 销毁session，让页面跳回注册页面

            } else {
                ResData resData = new ResData(1004,"密码修改失败，请重新修改",null);
                System.out.println(resData);
                resp.getWriter().write(JSON.toJSONString(resData));
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
